package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthDomainAim;
import orm.CollectivityWeekTimeBucket;
import util.DBHelper;

public class CMonthDomainAimDAO {
	
	public void insertCMonthDomainAim(Statement statement, 
			ArrayList<CollectivityMonthDomainAim> cmdaArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthDomainAim cmda = null;
		for (int i = 0; i < cmdaArr.size(); i++) {
			cmda = cmdaArr.get(i);
			statement.execute("insert into collectivitymonthdomainaim values('" + cMonthArrangeID + "'," +
					cmda.getDomainID() + ", " + cmda.getMonthAimID() + ", '" + cmda.getMonthAimDescribe() + "')");
		}
	}
	
	public void updateCMonthDomainAim(Statement statement,
			ArrayList<CollectivityMonthDomainAim> cmdaArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthDomainAim cmda = null;
		for (int i = 0; i < cmdaArr.size(); i++) {
			cmda = cmdaArr.get(i);
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update collectivitymonthdomainaim set MonthAimDescribe = '" + cmda.getMonthAimDescribe() + "'");
			sqlStr.append("where CMonthArrangeID = '");
			sqlStr.append(cmda.getCMonthArrangeID() + "' and DomainID = " + cmda.getDomainID() + " and MonthAimID = " + cmda.getMonthAimID() + "");
			
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteCMonthDomainAim(Statement statement, String cMonthArrangeID) throws SQLException {
		statement.execute("delete from collectivitymonthdomainaim where CMonthArrangeID='" + cMonthArrangeID + "'");
	}
	
	public ArrayList<CollectivityMonthDomainAim> getcmdaByCWAID(String cMonthArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<CollectivityMonthDomainAim> al = new ArrayList<CollectivityMonthDomainAim>();
		CollectivityMonthDomainAim cmda = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from collectivitymonthdomainaim where CMonthArrangeID='" + cMonthArrangeID + "'");
		
		while (rs.next()) {
			cmda = new CollectivityMonthDomainAim();
			int i = 1;
			cmda.setCMonthArrangeID(rs.getString(i++));
			cmda.setDomainID(rs.getInt(i++));
			cmda.setMonthAimID(rs.getInt(i++));
			cmda.setMonthAimDescribe(rs.getString(i++));
			al.add(cmda);
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
